package com.pozu.new_retail.service.impl;

import com.pozu.new_retail.dao.mapper.CommentMapper;
import com.pozu.new_retail.dao.mapper.FavoriteRecordMapper;
import com.pozu.new_retail.dao.mapper.GoodsMapper;
import com.pozu.new_retail.dao.mapper.OrderMapper;
import com.pozu.new_retail.dao.model.Goods;
import com.pozu.new_retail.dao.model.GoodsSimple;
import com.pozu.new_retail.dao.vo.BriefGoodsVo;
import com.pozu.new_retail.dao.vo.GoodsVo;
import com.pozu.new_retail.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private FavoriteRecordMapper favoriteRecordMapper;

    @Override
    public List<BriefGoodsVo> queryByMap(HashMap<String, Object> map) {
        return goodsMapper.queryByMap(map);
    }

    public List<GoodsSimple> queryGoodsListByIds(String goodIds) {
        return goodsMapper.queryGoodsListByIds(goodIds);
    }

    @Override
    public GoodsVo queryByGoodsId(int goodsId, long userId) {

        GoodsVo vo = goodsMapper.queryByGoodsId(goodsId);
        HashMap<String, Object> hashMap = new HashMap<String, Object>();

        hashMap.put("otherId", goodsId);
        hashMap.put("type", 1);
        hashMap.put("appId", vo.getAppId());

        /** 评论数 **/
        Integer commentNumber = commentMapper.queryCountByMap(hashMap);

        hashMap.put("type", 1);

        /** 收藏数量 **/
        Integer favoriteNumber = favoriteRecordMapper.queryCountByMap(hashMap);

        hashMap.put("usrId", userId);

        /** 是否收藏 **/
        Boolean favorite = favoriteRecordMapper.queryByMap(hashMap) == null ? false : true;

        /** 售卖数量(实际售卖数+虚构数) **/
        Integer saleNumber = vo.getAddNumber() + orderMapper.queryCountByGoodsId(goodsId);

        vo.setCommentNumber(commentNumber);
        vo.setFavoriteNumber(favoriteNumber);
        vo.setFavorite(favorite);

        vo.setSaleNumber(saleNumber);

        return vo;
    }

    @Override
    public Goods queryInfoByGoodsId(int goodsId) {
        return goodsMapper.queryInfoByGoodsId(goodsId);
    }

}
